package com.mingmen.canting.caigourenyuan;


import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mingmen.canting.R;
import com.mingmen.canting.model.ShoppingCartModel;
import com.mingmen.canting.model.SimpleData;
import com.mingmen.canting.util.MyStringUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * @author qdafengzi
 */
public class CaiGouDanZiAdapter extends RecyclerView.Adapter<CaiGouDanZiAdapter.ViewHolder> {
    private Context mContext;
    private List<ShoppingCartModel.DataBean.SupDataListBean.DetailsBean> mList=new ArrayList<>();

    private int mPosition;

    //定义一个点击事件的接口
    public interface OnitemClick {
        void onItemClick(View view, int position, List<ShoppingCartModel.DataBean.SupDataListBean.DetailsBean> mList);
    }

    private OnitemClick onitemClick;   //定义点击事件接口

    public void setOnitemClickLintener(OnitemClick onitemClick) {
        this.onitemClick = onitemClick;
    }

    public CaiGouDanZiAdapter(Context context, List<ShoppingCartModel.DataBean.SupDataListBean.DetailsBean> list, int position) {
        mContext = context;
        mList = list;
        mPosition = position;
    }

    /**
     * 新增方法
     *
     * @param position 位置
     */
    public void setPosition(int position) {
        this.mPosition = position;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_caigouren_zi, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {


        holder.tvOption.setText(mList.get(position).getCommodityName());
        holder.applyAmount.setText(mList.get(position).getApplyAmount() + "");
        holder.specs.setText(mList.get(position).getSpecs());
        holder.price.setText(mList.get(position).getPrice() + "");
        holder.text_fangqi.setTag(position);
        String cancelFlag = mList.get(position).getCancelFlag();
        if (cancelFlag.equals("0")) {
            holder.text_fangqi.setText("放弃");
            holder.text_fangqi.setTextColor(mContext.getResources().getColor(R.color.red_textcolor));
        } else {
            holder.text_fangqi.setText("恢复");
            holder.text_fangqi.setTextColor(mContext.getResources().getColor(R.color.normal_store));

        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        holder.checkbox.setChecked(mList.get(position).isSelect());

        holder.checkbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mList.get(position).setSelect(!mList.get(position).isSelect());
                onitemClick.onItemClick(v, position, mList);
            }
        });
        holder.text_fangqi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onitemClick.onItemClick(v, position, mList);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder {

        TextView tvOption;
        TextView text_fangqi;
        CheckBox checkbox;
        TextView applyAmount;
        TextView specs;
        TextView price;

        ViewHolder(View view) {
            super(view);


            tvOption = view.findViewById(R.id.tv_option);
            text_fangqi = view.findViewById(R.id.text_fangqi);
            checkbox = view.findViewById(R.id.checkbox);
            applyAmount = view.findViewById(R.id.applyAmount);
            specs = view.findViewById(R.id.specs);
            price = view.findViewById(R.id.price);
            //这里设置一个tag,作为被嵌套的recycleview item点击事件的 position


        }
    }
}